library Wave0 initializer init requires LegacySystem

    globals
        private trigger CivilianAITrigger
		private trigger CivilianAIFlee
		private trigger CivilianAIShotAt
		private group G = null
		
		private integer array ShotAtArrayCounter
		private boolean array Art15Given
		private integer ShotAtGlobalCounter = 0
		private trigger CivilianTrigger2
				
		private trigger CivilianStoryChildTrigger
		
		private boolean initFinished = false
		
		private boolean CivilianStoryEnabled = true
	endglobals

	private function Trig_Townsfolk_Actions takes nothing returns nothing
        local texttag t = null
		if udg_CurrentWave <= 1 and CivilianStoryEnabled then
			set t = CreateTextTag()
			set CivilianStoryEnabled = false
			call SetTextTagText(t, "|cfff3ad00Civilian|r : " + udg_CivilianChatter[GetRandomInt(1,20)], 0.02)
			call SetTextTagPos(t, GetUnitX(GetTriggerUnit()), GetUnitY(GetTriggerUnit()), 25.0)
			call SetTextTagVisibility(t,true)
			call SetTextTagLifespan(t,4.0)
			call SetTextTagPermanent(t,false)
			call TriggerSleepAction(4.2)
			set CivilianStoryEnabled = true
			set t = null
		endif
    endfunction
	
	private function Trig_Child_Story_Conditions takes nothing returns boolean
        return IsUnitType(GetTriggerUnit(),UNIT_TYPE_HERO)==true and udg_CurrentWave > 0 and udg_CurrentWave <= 3
    endfunction

    private function Trig_Child_Story_Actions takes nothing returns nothing
        local trigger t =  GetTriggeringTrigger()
        local sound snd = CreateSound( "Sound\\Dialogue\\HumanCampaign\\Human01\\H01VillagerC23.mp3", false, false, false, 10, 10, "DefaultEAXON" )
        call SetSoundParamsFromLabel(snd,"H01VillagerC23")
        call SetSoundDuration(snd,3422)
        call SetSoundVolume(snd,127)
        call DisableTrigger(t)
        if GetRandomInt(1,5)==1 and udg_InitialPlayers<4 then
            call TransmissionFromUnitWithNameBJ( udg_EchoCompany, gg_unit_nvlk_0040, "Kid", null, "You'll need this.", bj_TIMETYPE_ADD, 0, true )
        elseif GetRandomInt(1,5)==1 and udg_InitialPlayers>3 then
            call TransmissionFromUnitWithNameBJ( udg_EchoCompany, gg_unit_nvlk_0040, "Kid", snd, "But.. what about the others who were taken away?", bj_TIMETYPE_ADD, 0, true )
        endif
        call TriggerSleepAction(5.)
        call KillSoundWhenDone(snd)
        set snd = null
        set t = null
    endfunction

	private function GiveArt15ToAllOffenders takes nothing returns nothing
		local integer i = 0
		loop
			exitwhen i > 12
			if Art15Given[i] == false and ShotAtArrayCounter[i] >= 3 then
				set Art15Given[i] = true
				call GiveArticle15(Player(i), 0.95, GetPlayerName(Player(i)) + " has opened fire at civilians.")
			endif			
			set i = i + 1
		endloop
	endfunction
	
	private function MassFleeE takes nothing returns boolean
		local unit u = GetFilterUnit()
		call IssuePointOrderById(u, ORDER.move, GetRandomReal(-5000.0, 10000.0), GetRandomReal(-5000.0, 10000.0))
		if GetRandomInt(0,6)==1 then
			call SimpleChat(u,2.0, GetUnitName(u) + " : They are going to kill us all!" )
		elseif GetRandomInt(0,6)==1 then
			call SimpleChat(u,2.0, GetUnitName(u) + " : They are shooting us, run for your lives!" )
		endif
		set u = null
		return false
	endfunction
	
	private function MassFleeRemove takes nothing returns boolean
		call RemoveUnit(GetFilterUnit())
		return false
	endfunction
	
	private function CleanupLeaks takes nothing returns nothing
        call DisableTrigger(CivilianAITrigger)
		call DisableTrigger(CivilianAIFlee)
		call DisableTrigger(CivilianAIShotAt)
		call DisableTrigger(CivilianStoryChildTrigger)
		call DisableTrigger(CivilianTrigger2)
    endfunction
	
	private function MassFlee takes nothing returns nothing
		call GroupEnumUnitsOfPlayer(G,Player(9),Condition(function MassFleeE))
		call CleanupLeaks()
		call PolledWait(20.0)
		call GroupEnumUnitsOfPlayer(G,Player(9),Condition(function MassFleeRemove))
	endfunction
	
	private function CivilianAIShotAtActions takes nothing returns boolean
		local unit u = GetTriggerUnit()
		local unit a = null
		local player p = GetOwningPlayer(GetAttacker())
		local integer i = GetPlayerId(p)
		
		if udg_CurrentWave <= 3 and GetOwningPlayer(u) == Player(9) and i < 12 then
			set a = GetAttacker()
			
			if GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Arghh!" )
			elseif GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Nooo!" )
			elseif GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Please, don't shoot!" )
			endif
			
			if ShotAtGlobalCounter >= 4 then
				call GiveArt15ToAllOffenders()
			endif
			
			if ShotAtGlobalCounter == 7 then
				call MassFlee.execute()
			endif
			
			set ShotAtGlobalCounter = ShotAtGlobalCounter + 1
			set ShotAtArrayCounter[i] = ShotAtArrayCounter[i] + 1
			set a = null
		endif

		set p = null
		set u = null
		return false
	endfunction
	
	private function CivilianAIFleeAction takes nothing returns nothing
		local unit u = GetTriggerUnit()
		if GetOwningPlayer(u) == Player(9) and udg_CurrentWave <= 3 then
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssueImmediateOrderById(u, ORDER.stop)
		endif
		set u = null
	endfunction
	
    private function CivilianAI takes nothing returns nothing
        local unit u
        local group g
		
		set g = G
		
        call GroupEnumUnitsOfPlayer(g,Player(9),null)
        loop
            set u = FirstOfGroup(g)
            exitwhen u==null
            call TriggerSleepAction(0.)
            call ItemAI(u)
            if GetWidgetLife(u)<=GetUnitState(u,UNIT_STATE_MAX_LIFE)/1.75 and IsUnitType(u,UNIT_TYPE_MECHANICAL)!=true then
			
				if GetRandomInt(0,3)==1 then
					if GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : I'm injured!" )
					elseif GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : Help me!" )
					elseif GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : It hurts.." )
					else
						call SimpleChat(u,2.0, GetUnitName(u) + " : I don't want to die.." )
					endif
				endif

				if udg_CurrentWave <= 3 and GetUnitAbilityLevel(u, 'Abun') == 0 and GetWidgetLife(u)<=GetUnitState(u,UNIT_STATE_MAX_LIFE)/1.75 then
					call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
					call UnitAddAbility(u, 'Abun')
				endif
            endif
            call GroupRemoveUnit(g,u)
        endloop
		call GroupClear(g)
        set g = null
    endfunction

	private function initScript takes nothing returns nothing
		local trigger t
        set G = CreateGroup()
        set t = CreateTrigger()
		call TriggerRegisterTimerEvent(t,10.0,true)
        call TriggerAddAction(t,function CivilianAI)
		call DisableTrigger(t)
        set CivilianAITrigger = t
		
		set t = CreateTrigger()
		call TriggerRegisterEnterRectSimple( t, gg_rct_Residence41 )
		call TriggerAddAction(t,function CivilianAIFleeAction)
		call DisableTrigger(t)
		set CivilianAIFlee = t 
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_ATTACKED )
		call TriggerAddCondition(t, Condition(function CivilianAIShotAtActions))
		call DisableTrigger(t)
		set CivilianAIShotAt = t 

		set t = CreateTrigger()
        call TriggerRegisterEnterRectSimple(t, gg_rct_Home2)
        call TriggerAddCondition(t,Condition( function Trig_Child_Story_Conditions ))
        call TriggerAddAction(t,function Trig_Child_Story_Actions)
		call DisableTrigger(t)
        set CivilianStoryChildTrigger = t

		
		set t = CreateTrigger()
        call TriggerRegisterUnitEvent( t, gg_unit_nvl2_0022, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvl2_0023, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvl2_0014, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvlw_0016, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvlw_0013, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvlk_0010, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvk2_0020, EVENT_UNIT_SELECTED )
        call TriggerRegisterUnitEvent( t, gg_unit_nvk2_0021, EVENT_UNIT_SELECTED )
        call TriggerAddAction(t, function Trig_Townsfolk_Actions)
		call DisableTrigger(t)
        set CivilianTrigger2 = t

		set t = null
	endfunction

    private function Wave0 takes nothing returns nothing
        local unit u
        local integer i = 0
		local real x 
		local real y

		set x = -710.0
		set y = -115.0
        if udg_CurrentWave==-1 then
            set udg_CurrentWave=0
        else
            return
        endif
		
		if not initFinished then
			set initFinished = true
			call initScript()
		endif
		
        if udg_InitialPlayers<3 then
            call SetDynamicEvasionEnabled(true)
            set udg_GlobalUnit[100] = CreateUnit(Player(11),'z008',57.0,150.0,0.0)
            call SetLoadState(I2S(GetHandleId(udg_GlobalUnit[100])),1)
            call TriggerSleepAction(0.)
            if udg_InitialPlayers==1 then
                set udg_GlobalUnit[101] = CreateUnit(Player(11),'z008',-1251.0,348.0,0.0)
                call SetLoadState(I2S(GetHandleId(udg_GlobalUnit[101])),1)
            endif
        endif
        
		set ShotAtGlobalCounter = 0
		call EnableTrigger(CivilianAITrigger)
		call EnableTrigger(CivilianAIFlee)
		call EnableTrigger(CivilianAIShotAt)
		call EnableTrigger(CivilianStoryChildTrigger)
		call EnableTrigger(CivilianTrigger2)
		
        call CameraSetTargetNoiseForPlayer(GetLocalPlayer(),4.0,0.02)
        call PolledWait(8.0*udg_Pace)
        call MultiboardSetTitleText(udg_SquadStatus, "Insertion in Progress.." )
        if udg_Players>1 and GetRandomInt(1,15)==1 then
            call GeneralText(0.8, "|cfff3ad00Marine|r : Radio check." )
            call PolledWait(2.5)
            call GeneralText(0.8, "|cfff3ad00Marine #2|r : I read you." )
        endif
        call PolledWait(12.*udg_Pace)
        call MultiboardSetTitleText(udg_SquadStatus, "Insertion Complete" )
        if udg_InitialPlayers==1 then //50% less fractures in solo
            loop
                exitwhen i>10
                set udg_AilmentChance[(i*7)+4] = 16
                set i=i+1
            endloop
        endif
        call GeneralText(4.0, "|cffcd950cBattalion|r : Head to Ravenholm and gather intel. The town has some medical supplies." )
        call PolledWait(7.)
        call StartSound(gg_snd_SatelliteImaging)
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0.0, 0.0, 3.0, "|cffcd950cALICE|r : Satellite image of Primary Objective captured." )
        call PingMinimapEx(x,y,10.0,255,0,0,false)
        call SetCameraQuickPosition(x,y)
        call MultiboardSetTitleText( udg_SquadStatus, "Patrol Sector E." )
        set i = 1
        loop
            exitwhen i>udg_Players
            set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e002',x,y,0.0)
            call UnitApplyTimedLife(u,'Bhwd',30.0)
            call TriggerSleepAction(0.)
            set i=i+1
        endloop
        call CameraSetSourceNoise(10.0,0.10)
        call PolledWait(6.)
        call GeneralText(8.0, "|cffcd950cBattalion|r : This is a routine patrol and we don't want nothing fancy. Get this done quick and easy. We have hot chow waiting for you back at base." )
        call PolledWait(10.0*udg_Pace)
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Patrol Perimeter" )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Patrol Perimeter." )
        call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))-0.1)
        call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))-0.1)
        set u = null
        
		call ExecuteRegisteredFunction("Wave1a") //go to Wave1a
		
		call PolledWait(30.0)
		call DisableTrigger(CivilianTrigger2)
    endfunction
	
    private function init takes nothing returns nothing
        call RegisterFunction("Wave0", function Wave0)
        call RegisterFunction("Wave0.CleanupLeaks", function CleanupLeaks)
    endfunction
    
endlibrary